<?php

namespace System\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\Session\Container;
use System\Model\Utility;
use System\Model\Imagespro;

class MyimagesController extends AbstractActionController {

    private function checklogin() {
        $session = new Container("ssadmin");
        $username = $session->adminusername;
        if ($username == null) {
            $this->redirect()->toUrl(WEB_PATH . "/cpadmin");
        }
    }

    private $ImagesTable;

    private function getImagesTable() {
        if (!$this->ImagesTable) {
            $pst = $this->getServiceLocator();
            $this->ImagesTable = $pst->get("System\Model\ImagesTable");
        }
        return $this->ImagesTable;
    }

    public function indexAction() {

        $this->checklogin();
        $this->layout("layout/layoutadmin");
    }

    public function loadmydirAction() {
        $listdir = $this->getImagesTable()->loadDir();
        $view = "";
        if (count($listdir) > 0) {
            foreach ($listdir as $key => $listdata) {
                $view .= '<div class="myfiles" ondblclick="loadfiles('.$key.');">
                        <div class="iconmyfile"><i class="fa fa-folder-open"></i></div>
                        <span>' . $listdata["imt_Dir"] . '</span>
                    </div><input type="hidden" id="dir'.$key.'" value="' . $listdata["imt_Dir"] . '"/>';
            }
        } else {
            $view = "Chưa có thư mục nào.";
        }
        echo $view;
        die;
    }

    public function loadfileAction() {
        $dir=  addslashes(trim($this->params()->fromPost("dir")));
        $listfiles = $this->getImagesTable()->loadfile($dir);
        $view = "";
        if (count($listfiles) > 0) {
            foreach ($listfiles as $key => $listdata) {
                $view .= '<div class="myfiles2" id="' . $listdata["img_Id"] . '">
                    <span onclick="delimages(' . $listdata["img_Id"] . ');"><i class="fa fa-times"></i></span>
                        <img src="' . WEB_PATH . '/public/' . $listdata["img_thumb"] . '"/>
                      </div>';
            }
        } else {
            $view = "Chưa có files nào trong thư mục này.";
        }
        echo $view;
        die;
    }
    
    public function delfilesAction(){
        $filesId=  addslashes(trim($this->params()->fromPost("imgid")));
        $this->getImagesTable()->delfile($filesId);
         echo "delok*duypc*Xóa file thành công.";
         die;
    }

}
